package com.put.mybills.adapter;

import java.io.File;
import java.nio.ByteBuffer;
import java.util.List;

import com.put.mybills.R;
import com.put.mybills.model.Facture;


import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Log;
import android.util.LruCache;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class FactureListAdapter  extends ArrayAdapter<String>  {
	
	private final Context context;
	private final String[] values;
	private List<Facture> factures;
	private Boolean ifReadIcon;
	
	private final LruCache<Integer, Bitmap> mMemoryCache;


	public FactureListAdapter(Context context, String[] values, Boolean ifReadIcon) {
		super(context, R.drawable.facture_list_item, values);
		this.context = context;
		this.values = values;	
		this.ifReadIcon=ifReadIcon;
		
		final int cacheSize = (int) (Runtime.getRuntime().maxMemory() / 1024);
		mMemoryCache = new LruCache<Integer, Bitmap>(cacheSize) {
			@Override
			protected int sizeOf(final Integer key, final Bitmap bitmap) {				
				return bitmap.getRowBytes() * bitmap.getHeight() / 1024;
			}
		};
		
		
	}
	
	public void refreshValues(String[] values) {		
		
		this.addAll(values);
		this.notifyDataSetChanged();		
	}
	
	
	public List<Facture> getFactures() {
		return factures;
	}

	public void setFactures(List<Facture> factures) {
		this.factures = factures;
	}
	
	private void addBitmapToMemoryCache(final int key, final Bitmap bitmap) {
		if (getBitmapFromMemCache(key) == null) {
			mMemoryCache.put(key, bitmap);
		}
	}
	
	private Bitmap getBitmapFromMemCache(final int key) {
		return mMemoryCache.get(key);
	}
	
	
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		LayoutInflater inflater = (LayoutInflater) context
			.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
 
		View rowView = inflater.inflate(R.drawable.facture_list_item, parent, false);
		TextView secondLine = (TextView) rowView.findViewById(R.id.secondLine);
		TextView firstLine = (TextView) rowView.findViewById(R.id.firstLine);
		ImageView imageView = (ImageView) rowView.findViewById(R.id.icon);
		
		if (ifReadIcon) {
			String path = factures.get(position).getIcon();
			
			Bitmap bitmap1 = getBitmapFromMemCache(factures.get(position).getId());
			if (bitmap1 != null) {
				imageView.setImageBitmap(bitmap1);					
			}		
			
			else if (path != "" && path!=null)
			{
				Log.d("ICON TEST DEBUG",path);
				File file = new File(path);
				if (file.exists()){
					Bitmap bitmap = BitmapFactory.decodeFile(path);
					
					if (bitmap != null ) {
						int bytes = bitmap.getByteCount();  
			            ByteBuffer buffer = ByteBuffer.allocate(bytes); 
			            bitmap.copyPixelsToBuffer(buffer);
			            byte[] FactureByteArray = buffer.array();           
			    		Bitmap b = BitmapFactory.decodeByteArray(FactureByteArray, 0, FactureByteArray.length);					
						Bitmap resizedBitmap = Bitmap.createScaledBitmap(bitmap, 120, 120, false);
						addBitmapToMemoryCache(factures.get(position).getId(), resizedBitmap);
			    		imageView.setImageBitmap(resizedBitmap); 
					}
				}
				else {
					//Log.d("ICON TEST DEBUG","PLIK NIE ISTNIEJE" );
				}
				
			}
		}
		
		
		firstLine.setText(values[position]);
		secondLine.setText(factures.get(position).getDescripiton());		
		String s = values[position];	
		//imageView.setImageResource(R.drawable.ic_cloud);
	
 
		return rowView;
	}

}
